package com.employee.controller;

import com.employee.pojo.dto.PwdDTO;
import com.employee.pojo.dto.UserDTO;
import com.employee.pojo.entry.User;
import com.employee.pojo.result.Result;
import com.employee.pojo.vo.UserInfoVO;
import com.employee.pojo.vo.UserVO;
import com.employee.service.UserService;
import com.employee.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    // 登录
    @PostMapping("/login")
    public Result<UserVO> login(@RequestBody UserDTO userDTO) {
        User user = userService.login(userDTO);

        Map<String, Object> claims = new HashMap<>();
        claims.put("id", user.getEmpId());
        claims.put("email", user.getEmail());

        String token = JwtUtils.getJwt(claims);
        UserVO userVO = new UserVO();
        userVO.setEmpId(user.getEmpId());
        userVO.setUsername(user.getUsername());
        userVO.setEmail(user.getEmail());
        userVO.setHeadImg(user.getHeadImg());
        userVO.setToken(token);

        System.out.println(userVO);
        return Result.success(userVO);
    }

    // 获取用户信息
    @GetMapping("/{id}")
    public Result<UserInfoVO> getUserInfo(@PathVariable("id") Long id) {

        return Result.success(userService.getUserInfo(id));
    }

    // 更新密码
    @PostMapping("/updatePwd")
    public Result<String> updatePwd(@RequestBody PwdDTO pwdDTO) {
        userService.updatePwd(pwdDTO);
        return Result.success("密码修改成功");
    }
}
